package com.david.trade.clear.component;


import com.david.trade.clear.service.TcpMessage;
import com.lmax.disruptor.EventHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author admin
 */
@Slf4j
public class TcpMessageEventHandler implements EventHandler<TcpMessageEvent> {

    private boolean closed;

    @Override
    public void onEvent(TcpMessageEvent tcpMessageEvent, long sequence, boolean endOfBatch) throws Exception {
        log.info("event: {} , sequence: {} , endOfBatch: {}", tcpMessageEvent.get().toString(), sequence, endOfBatch);
        if (endOfBatch && !closed) {
            TcpMessage tcpMessage = tcpMessageEvent.get();
            tcpMessage.getTcpHandler().reply(tcpMessage.getMsgId(), "receive:" + tcpMessage.getMsg());
        }
    }

    public void close() {
        closed = true;
    }
}
